# @shuaige : 陈世玉
# @name :TimeLine.py
# @time :2024/12/9 10:50
from pyecharts.charts import Bar
from pyecharts.charts import Timeline
from pyecharts.options import *
from pyecharts.globals import ThemeType
f=open("data.csv",'r',encoding='utf-8')
data=f.readlines()
f.close()
data.pop(0)#去掉第一行
#封装一个字典对象
data_dict={}
for i in data:
    i=i.split(",")
    # print(i)
    # print(i)
    month=i[0]
    brand=i[2]
    selecount=int(i[4])
    try :
        data_dict[month].append([brand,selecount])
    except KeyError:
        data_dict[month]=[]
        data_dict[month].append([brand, selecount])
timeline=Timeline({"theme":ThemeType.LIGHT})
sorted_months_list=['2024-5', '2024-6', '2024-7', '2024-8', '2024-9','2024-10','2024-11']
print(sorted_months_list)
for month in sorted_months_list:
    data_dict[month].sort(key=lambda x:x[1],reverse=True)
    month_data=data_dict[month][:15]
    x_data=[]
    y_data=[]
    for brand_selecount in month_data:
        x_data.append(brand_selecount[0])
        y_data.append(brand_selecount[1])

    bar=Bar()
    x_data.reverse()
    y_data.reverse()
    bar.add_xaxis(x_data)
    bar.add_yaxis("销量统计(辆)",y_data, label_opts=LabelOpts(position="right"))
    bar.reversal_axis()
    bar.set_global_opts(title_opts=TitleOpts(title=month+"汽车销量前十五名"))
    timeline.add(bar,month)
    timeline.add_schema(
        is_timeline_show=True,  # 是否显示时间线
        is_auto_play=True,  # 是否自动播放
        is_loop_play=True,  # 是否循环播放
        play_interval=2000  # 自动播放的时间间隔 单位为毫秒

    )
    timeline.render("汽车动态销量.html")
